import request from "../../utils/request"
import Vue from "vue"
export default {
    namespaced: true,
    state: {
        data: [],
        showAlert: false, //控制是否显示添加弹框
        showAmendAlert: false, //控制是否显示修改弹框
        showDetailsAlert: false, //控制是否显示详情弹框
    },
    getters: {
        
    },
    mutations: {
        // 获取数据
        setData(state, res) {
            state.data = res.map((item, index) => {
                return {
                    id: item.id,
                    name: item.name,
                    organization: item.organization,
                    creator: item.creator,
                    createTime: item.createTime, 
                    state: item.state,
                    isCheck: false,
                }
            })
        },
        // 设置添加弹框
        setAlert(state, flag) {
            state.showAlert = flag
        },
        // 设置修改弹框
        setAmendAlert(state, flag) {
            state.showAmendAlert = flag
        },
        // 设置查看详情弹框
        setDetailsAlert(state, flag) {
            state.showDetailsAlert = flag
        },
        // 模糊搜索
        searchFun(state, res) {
            state.data = state.data.filter((item, index) => {
                if (item.name.indexOf(res.name) !== -1 && item.state.indexOf(res.state) !== -1) {
                    return item
                }
            })
        },

        // 全选反选
        setCheckAll(state, index) {
            state.data[index].isCheck = !state.data[index].isCheck
        }
    },
    actions: {
        // 获取数据
        async getData(context) {
            let r = await request({
                url: "/getRoelData",
                method: "GET",
                params: {},
            }).then((res) => {
                return res
            })
            context.commit('setData', r.data)
            return r
        },
        // 添加数据
        async addData(context, res) {
            await request({
                url: "/addData",
                method: "POST",
                data: res,
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        },

        // 修改数据
        async amendData(context, res) {
            await request({
                url: "/amendData",
                method: "POST",
                data: res,
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        },
        // 删除数据
        async deleteData(context, res) {
            await request({
                url: "/deleteData",
                method: "POST",
                data: {
                    id: res
                },
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        },
        // 停用，修改state
        async setState(context, res) {
            await request({
                url: "/blockUpState",
                method: "POST",
                data: res,
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        },
        // 批量停用，修改state
        async setStates(context, res) {
            console.log(res);
            await request({
                url: "/blockUpStates",
                method: "POST",
                data: res,
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        },
        // 批量删除
        async batchDeletes(context, res) {
            console.log(res);
            await request({
                url: "/batchDelete",
                method: "POST",
                data: res,
            }).then((res) => {
                console.log(res);
            })
            context.dispatch('getData')
        }
    },
}